// pages/songDetail.js
import Pubsub from 'pubsub-js'
import Moment from 'moment'
import {getSongDetail,getSongUrl} from '~/service/songDetail'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlay:false,
    id:'', // 音乐id
    songInfo:{}, //音乐详情
    songUrl:'', //音乐播放地址
    currentTIme:'', //当前音乐播放时间
    durationTime:'', //音乐总时长
    currentProWidth:'' //当前进度条长度
  },

  // 获取歌曲详细信息
  async getSongDetailData(){
    let cookie = JSON.parse(wx.getStorageSync('cookie'))
    let {songs} = await getSongDetail({ids:this.data.id,cookie});
    let durationTime = Moment(songs[0].dt).format('mm:ss')
    this.setData({
      songInfo:songs[0] || '',
      durationTime
    })
    wx.setNavigationBarTitle({
      title: this.data.songInfo.al.name,
    })
  },
  // 获取歌曲播放地址
  async getSongUrlData(){
    let cookie = JSON.parse(wx.getStorageSync('cookie'))
    let {data} = await getSongUrl({id:this.data.id,level:'lossless',cookie});
    this.setData({
      songUrl:data[0].url || ''
    })
  },
    // 控制播放
    handleMusicPlay(){
      let isPlay = !this.data.isPlay
      this.setData({
        isPlay
      })
      // 判断是否为播放状态,如果是则修改音乐标题
      if(this.data.isPlay){
        this.backgroundAudioManager.src = this.data.songUrl;
        this.backgroundAudioManager.title = this.data.songInfo.al.name;
      }else{
        this.backgroundAudioManager.pause()
      }
    },
    // 歌曲切换控制
     handleSwitch(e){
      let type = e.currentTarget.id;  //获取切换类型
      Pubsub.publish('switchType',type)
      Pubsub.subscribe('musicId',(msg,data)=>{
        this.setData({
          id:data
        })
        this.getSongDetailData();
        this.getSongUrlData();
        this.getSongUrlData().then(()=>{
          this.backgroundAudioManager.src = this.data.songUrl;
          this.backgroundAudioManager.title = this.data.songInfo.al.name;
        })
        this.setData({
          isPlay:true
        })
        Pubsub.unsubscribe('musicId')
      })

    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.backgroundAudioManager = wx.getBackgroundAudioManager();
    this.setData({
      id:options.songid,
    })
    this.getSongDetailData();
    this.getSongUrlData();

    // 监听音频暂停时的回调
    this.backgroundAudioManager.onPause(()=>{
      this.setData({
        isPlay:false
      })
    })
    // 监听音频播放时的回调
    this.backgroundAudioManager.onPlay(()=>{
      this.setData({
        isPlay:true
      })
    })

    // 监听音频停止时的回调
    this.backgroundAudioManager.onStop(()=>{
      this.setData({
        isPlay:false
      })
    })
    // 监听音频播放更新时间
    this.backgroundAudioManager.onTimeUpdate(()=>{
      // 当前进度条的长度 等于 当前播放进度与播放总时长的百分比 * 
      let currentTIme = Moment(this.backgroundAudioManager.currentTime).format('mm:ss');
      let currentProWidth = this.backgroundAudioManager.currentTime / this.backgroundAudioManager.duration * 450;
      this.setData({
        currentTIme,
        currentProWidth
      })
  })

  // 监听音频播放结束
  this.backgroundAudioManager.onEnded(()=>{
    console.log('播放结束');
    Pubsub.subscribe('musicId',(msg,data)=>{
      this.setData({
        id:data
      })
      this.getSongDetailData();
      this.getSongUrlData();
      this.getSongUrlData().then(()=>{
        this.backgroundAudioManager.src = this.data.songUrl;
        this.backgroundAudioManager.title = this.data.songInfo.al.name;
      })
      this.setData({
        isPlay:true
      })
      Pubsub.unsubscribe('musicId')
    })
    Pubsub.publish('switchType','next')
    this.setData({
      currentProWidth:0
    })
  })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})